<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
		<title>Simple Input-Output Data Binding Example</title>
		<style type="text/css">
			@import "css/app-format.css";
			@import "../../../dijit/themes/claro/claro.css";
		</style>

		<script type="text/javascript" data-dojo-config="parseOnLoad:0,isDebug:1,async:1, mvc: {debugBindings: 1}" src="../../../dojo/dojo.js"></script>
		<script type="text/javascript">
			var model;

			require([
				"doh/runner",
				"dojo/dom",
				"dojo/parser",
				"dojo/when",
				"dijit/registry",
				"dojox/mvc/getStateful",
				"dijit/form/TextBox",
				"dijit/form/Button",
				"dojox/mvc/Group",
				"dojox/mvc/Output"
			], function(doh, ddom, parser, when, registry, getStateful){
				// The dojox.mvc.StatefulModel class creates a data model instance
				// where each leaf within the data model is decorated with dojo.Stateful
				// properties that widgets can bind to and watch for their changes.
				//model = mvc.newStatefulModel({data :{"First": "John", "Last": "Doe", "Email": "jdoe@example.com"}});
				model = getStateful({"First": "John", "Last": "Doe", "Email": "jdoe@example.com"});
				// the StatefulModel created above is initialized with 
				// model.First set to "John", model.Last set to "Doe" and model.Email set to "jdoe@example.com"
				when(parser.parse(), function(){
					doh.register("Synchronize text box and label", [{
						name: "Check first value",
						runTest: function(){
							doh.is("John", registry.byId("firstnameInput").get("value"), "First should reflect initial data");
							doh.is("Doe", registry.byId("lastnameInput").get("value"), "Last should reflect initial data");
							doh.is("jdoe@example.com", registry.byId("emailInput").get("value"), "Email should reflect initial data");
							doh.t(/\s*\(first name is: John\)\s*/, registry.byId("firstnameOut").innerHTML, "First should reflect initial data");
							doh.t(/\s*\(last name is: Doe\)\s*/, registry.byId("lastnameOut").innerHTML, "Last should reflect initial data");
							doh.t(/\s*\(email is: Doe\)\s*/, registry.byId("emailOut").innerHTML, "Email should reflect initial data");
						}
					}, {
						name: "Check value sync",
						runTest: function(){
							model.set("First", "JohnJohn");
							doh.t(/\s*\(first name is: JohnJohn\)\s*/, registry.byId("firstnameOut").innerHTML, "First should reflect the change");
						}
					}, {
						name: "Check model reset",
						setUp: function(){
							model.set({"First": "John", "Last": "Doe", "Email": "jdoe@example.com"});
							
							//model.reset();
						},
						runTest: function(){
							doh.is("John", registry.byId("firstnameInput").get("value"), "First should reflect initial data");
							doh.is("Doe", registry.byId("lastnameInput").get("value"), "Last should reflect initial data");
							doh.is("jdoe@example.com", registry.byId("emailInput").get("value"), "Email should reflect initial data");
							doh.t(/\s*\(first name is: John\)\s*/, registry.byId("firstnameOut").innerHTML, "First should reflect initial data");
							doh.t(/\s*\(last name is: Doe\)\s*/, registry.byId("lastnameOut").innerHTML, "Last should reflect initial data");
							doh.t(/\s*\(email is: Doe\)\s*/, registry.byId("emailOut").innerHTML, "Email should reflect initial data");
						}
					}]);

					doh.run();
				});
			});
		</script>
	</head>
	<body class="claro">
		<script type="dojo/require">at: "dojox/mvc/at"</script>
		<div id="wrapper">
			<div id="header">
				<div id="navigation"></div>
				<div id="headerInsert">
					<h1>Input Ouput Sync</h1>
					<h2>Data Binding Example</h2>
				</div>
			</div>
			<div id="main">
				<div id="leftNav"></div>
				<div id="mainContent">
					<div class="row">
						<label class="cell" for="firstnameInput">First:</label>
						<input class="cell" id="firstnameInput" data-dojo-type="dijit.form.TextBox"
							data-dojo-props="value: at(model, 'First')"></input>
						<!-- Content in output below will always be in sync with value of textbox above -->
						<span id="firstnameOut" data-dojo-type="dojox.mvc.Output" 
												data-dojo-props="value: at(model, 'First')">
							(first name is: ${this.value})
						</span>
					</div>
					<div class="row">
						<label class="cell" for="lastnameInput">Last:</label>
						<input class="cell" id="lastnameInput" data-dojo-type="dijit.form.TextBox"
										data-dojo-props="value: at(model, 'Last')"></input>
						<span id="lastnameOut" data-dojo-type="dojox.mvc.Output" 
												data-dojo-props="value: at(model, 'Last')">
							(last name is: ${this.value})
						</span>
					</div>
					<div class="row">
						<label class="cell" for="emailInput">Email:</label>
						<input class="cell" id="emailInput" data-dojo-type="dijit.form.TextBox"
										data-dojo-props="value: at(model, 'Email')"></input>
						<span id="emailOut" data-dojo-type="dojox.mvc.Output" 
											data-dojo-props="value: at(model, 'Email')">
							(email is: ${this.value})
						</span>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
